<template>
  <el-dialog
    :title="title"
    :visible="visible"
    :append-to-body="true"
    @close="handleDialogClose"
    width="600px"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-position="right"
      label-width="100px"
      class="form"
    >
      <el-form-item label="角色名称:" prop="roleName">
        <el-input class="form-item" v-model="form.roleName" placeholder="请输入角色名称"></el-input>
      </el-form-item>
      <el-form-item label="角色值:" prop="roleValue">
        <el-input class="form-item" v-model="form.roleValue" placeholder="请输入角色值"></el-input>
      </el-form-item>
      <el-form-item label="角色描述:" prop="description">
        <el-input class="form-item" v-model="form.description" placeholder="请输入角色描述"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import RoleService from '../../../../service/role'

export default {
  props: {
    title: {
      type: String,
      required: true,
      default: () => ''
    },
    show: {
      type: Boolean,
      required: true,
      default: () => false
    },
    data: {
      type: Object,
      required: false,
      default: () => {}
    }
  },
  data() {
    // 角色名称验证
    const checkRoleName = async (rule, value, callback) => {
      if (value.length === 0) {
        return callback(new Error('请输入角色名称'))
      } else {
        const result = await this.getIsExistsByKeyword(value)
        if (result.data) {
          return callback(new Error('该角色名称已被注册'))
        }
        callback()
      }
    }

    // 角色值验证
    const checkRoleValue = async (rule, value, callback) => {
      if (value.length === 0) {
        return callback(new Error('请输入角色值'))
      } else {
        const resule = await this.getIsExistsByKeyword(value)
        if (resule.data) {
          return callback(new Error('该角色值已被注册'))
        }
        callback()
      }
    }

    return {
      visible: false,
      form: {
        roleId: '',
        roleName: '',
        roleValue: '',
        description: ''
      },
      rules: {
        roleName: { required: true, validator: checkRoleName, trigger: 'blur' },
        roleValue: {
          required: true,
          validator: checkRoleValue,
          trigger: 'blur'
        },
        description: {
          required: true,
          message: '角色描述不能为空',
          trigger: 'blur'
        }
      }
    }
  },
  methods: {
    // 处理Dialog关闭事件
    handleDialogClose() {
      this.$emit('update:show')
      this.form = {
        roleId: '',
        roleName: '',
        roleValue: '',
        description: ''
      }
      this.$refs.form.resetFields()
    },
    // 根据关键字查询角色是否存在
    getIsExistsByKeyword(keyword) {
      return RoleService.getIsExistsByKeyword(keyword, this.form.roleId)
    },
    // 提交表单
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.roleId) {
            // 清空 permissions 属性
            delete this.form.permissions

            RoleService.updateRole(this.form).then(res => {
              // 提示
              this.$message.success(res.message)
              // 关闭Dialog
              this.visible = false
              // 派发事件刷新Table
              this.$emit('success', null)
            })
          } else {
            RoleService.insertRole(this.form).then(res => {
              // 提示
              this.$message.success(res.message)
              // 关闭Dialog
              this.visible = false
              // 派发事件刷新Table
              this.$emit('success', 1)
            })
          }
        }
      })
    }
  },
  watch: {
    show(value) {
      this.visible = value
    },
    data(value) {
      this.form = value || {
        roleId: '',
        roleName: '',
        roleValue: '',
        description: ''
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
.form
  margin 0 30px
  .form-item
    width 300px
</style>
